import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { OperType } from 'src/global-type';

export type SystemLogDocument = SystemLog & Document;

@Schema()
export class SystemLog {
  @Prop({ required: true, type: String })
  userId: string; // 执行操作的用户的id

  @Prop({ required: true, type: String })
  account: string; // 执行操作的用户的账号

  @Prop({ required: true, type: String })
  userName: string; // 执行操作的用户的用户名

  @Prop({ type: String })
  ownerId: string;

  @Prop({ required: true, type: String })
  operType: OperType; // 执行的操作类型

  @Prop({
    required: true,
    type: Date,
    default: Date.now,
    index: { expires: 2592000 },
  }) // 30天后过期
  operTime: Date; // 执行操作的时间

  @Prop({ required: true, type: String })
  ip: string; // 执行操作的ip地址

  @Prop({ required: true, type: String })
  description: string; // 描述

  @Prop({ type: String })
  targetType: string; // 目标类型

  @Prop({ type: String })
  target: string; // 目标id

  @Prop({ type: String, default: '' })
  oldData: string; // 更改前数据JSON字符串

  @Prop({ type: String, default: '' })
  newData: string; // 更改后数据JSON字符串
}

export const SystemLogSchema = SchemaFactory.createForClass(SystemLog);
SystemLogSchema.index({ operTime: -1 });
